package com.ding.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class StreamChannel extends Channel {


    private InputStream inputStream;
    private OutputStream outputStream;

    public InputStream getInputStream() {
        return inputStream;
    }

    public OutputStream getOutputStream() {
        return outputStream;
    }

    public StreamChannel() {
        setClosed(true);
    }

    public StreamChannel(Socket socket) {
        super(socket);
        if (getSocket() == null) {
            setClosed(true);
            return;
        }
        try {
            this.inputStream = socket.getInputStream();
            this.outputStream = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setSocket(Socket socket) {
        super.setSocket(socket);
        if (getSocket() == null) {
            return;
        }
        try {
            this.inputStream = socket.getInputStream();
            this.outputStream = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void close() {
        super.close();
        this.inputStream = null;
        this.outputStream = null;
    }
}
